package com.humanres.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciko.code.adapter.WorkFlowAdapter;
import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.RecodeListEntity;
import com.ciko.code.bean.User;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.AgreeLeaveFlowReq;
import com.ciko.code.http.Req.BackLeaveFlowReq;
import com.ciko.code.http.Req.FinishLeaveFlowReq;
import com.ciko.code.http.Req.GetLeaveFlowNextPerson;
import com.ciko.code.type.ResLeaveEnum;
import com.ciko.code.utils.DateUtil;
import com.ciko.code.utils.Util;
import com.ciko.code.views.PullListView;
import com.humanres.http.Req.GetLeaveFlowsBySelfEty;
import com.mywork.R;
import com.mywork.WorkUIKit;
import com.ui.dialog.ResDialog;
import com.ui.dialog.ResDialogListener;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ciko on 2015/12/9
 */
public class ResLeaveDetailActivity
        extends
        BaseActicity
{
	
	private TextView			   tvnamePerson;
	private TextView			   tvtypeLeave;
	private TextView			   tvcreateTimeLeave;
	private TextView			   tvaddLeave;
	private TextView			   tvstartTimeLeave;
	private TextView			   tvendTimeLeave;
	private TextView			   tvresonLeave;
	private TextView			   tvStateLeave;
	private TextView			   tvDaysLeave;
	private LinearLayout		   llAcceptOrReject;
	private LinearLayout		   llFinish;
	private Button				   btnReject;
	private Button				   btnAccept;
	private Button				   btnFinish;
	private GetLeaveFlowsBySelfEty getLeaveFlowsBySelfEty;
								   
	private PullListView		   lv_workFlow;
	private WorkFlowAdapter		   workFlowAdapter;
	private List<RecodeListEntity> recodeListEntityList	= new ArrayList<>();
	private ResLeaveEnum		   fromtype;
								   
	private User				   nextuser;
	private ResDialog			   resDialog;
	private boolean				   isGetNextUser		= false;			//nextuser获取成功
	private LinearLayout		   ll;
								   
	@Override
	protected void initTopBar() {
		getLeaveFlowsBySelfEty = (GetLeaveFlowsBySelfEty) getIntent().getSerializableExtra("GetLeaveFlowsBySelfEty");
		fromtype = (ResLeaveEnum) getIntent().getSerializableExtra("fromType");
		recodeListEntityList = getLeaveFlowsBySelfEty.getWorkFlow().getRecodeList();
		setTitle("查看请假详情");
		enableNavLeftImg();
	}
	
	@Override
	protected void initLayout() {
		setContentView(R.layout.activity_res_item_leave_detail);
	}
	
	@Override
	protected void initView() {
		ll = (LinearLayout) findViewById(R.id.ll_leave);
		ll.setVisibility(View.GONE);
		tvDaysLeave = (TextView) findViewById(R.id.tv_daysLeave);
		tvresonLeave = (TextView) findViewById(R.id.tv_resonLeave);
		tvendTimeLeave = (TextView) findViewById(R.id.tv_endTimeLeave);
		tvstartTimeLeave = (TextView) findViewById(R.id.tv_startTimeLeave);
		tvaddLeave = (TextView) findViewById(R.id.tv_addLeave);
		tvcreateTimeLeave = (TextView) findViewById(R.id.tv_createTimeLeave);
		tvtypeLeave = (TextView) findViewById(R.id.tv_typeLeave);
		tvnamePerson = (TextView) findViewById(R.id.tv_namePerson);
		tvStateLeave = (TextView) findViewById(R.id.tvStateLeave);
		
		llAcceptOrReject = (LinearLayout) findViewById(R.id.ll_acceptOrreject);
		llFinish = (LinearLayout) findViewById(R.id.ll_finish);
		btnReject = (Button) findViewById(R.id.btnReject);
		btnAccept = (Button) findViewById(R.id.btnAccept);
		btnFinish = (Button) findViewById(R.id.btnfinish);
		progressDialog2.show();
		//        if (null != fromtype && fromtype == ResLeaveEnum.Receive) {
		//            if (getLeaveFlowsBySelfEty.getUserId() == getLeaveFlowsBySelfEty.getNextUserId()) {
		//                llAcceptOrReject.setVisibility(View.GONE);
		//                llFinish.setVisibility(View.VISIBLE);
		//            } else {
		//                llAcceptOrReject.setVisibility(View.GONE);
		//                llFinish.setVisibility(View.VISIBLE);
		//            }
		//            if (WorkUIKit.getUser().getId() == getLeaveFlowsBySelfEty.getNextUserId()) {
		//                llAcceptOrReject.setVisibility(View.VISIBLE);
		//            } else {
		//                llAcceptOrReject.setVisibility(View.GONE);
		//            }
		//        } else {
		//            llAcceptOrReject.setVisibility(View.GONE);
		//            llFinish.setVisibility(View.GONE);
		//        }
		//
		//        if ("流程结束".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || WorkUIKit.getUser().getId() == getLeaveFlowsBySelfEty.getNextUserId()) {
		//            llAcceptOrReject.setVisibility(View.GONE);
		//        }
		if (null != fromtype && fromtype == ResLeaveEnum.Receive)
		{
//			if ("归档".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || ("流程结束".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) && nextuser == null) || "失败".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || "成功".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || getLeaveFlowsBySelfEty.getNextUserId() != WorkUIKit.getUser().getId())
			if ("归档".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || "失败".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || "审批不通过".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || getLeaveFlowsBySelfEty.getNextUserId() != WorkUIKit.getUser().getId())

				{
				llAcceptOrReject.setVisibility(View.GONE);
				llFinish.setVisibility(View.GONE);
				
			}
			else
			{
				llAcceptOrReject.setVisibility(View.VISIBLE);
				llFinish.setVisibility(View.GONE);
			}

			if (("流程发起".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || "成功".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || "审批中".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName())) && WorkUIKit.getUser().getUserPositionMark() == 12)
			{
				btnReject.setVisibility(View.GONE);
				btnAccept.setText("归档");
			}
			
		}
		else
		{
			llAcceptOrReject.setVisibility(View.GONE);
			llFinish.setVisibility(View.GONE);
		}
		lv_workFlow = (PullListView) findViewById(R.id.lv_workFlow);
	}
	
	@Override
	protected void initAdapter() {
		boolean isLast = "归档".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName())?true:false;
		workFlowAdapter = new WorkFlowAdapter(this, recodeListEntityList, R.layout.item_list_res_work_flow, getLeaveFlowsBySelfEty.getWorkFlow(), isLast);
		lv_workFlow.setAdapter(workFlowAdapter);
	}
	

	@Override
	protected void initDates() {
//		tvDaysLeave.setText(Util.getDayAndHour((int) (getLeaveFlowsBySelfEty.getLeaveDays())));
		tvDaysLeave.setText(getLeaveFlowsBySelfEty.getLeaveDays() + "天");
		tvnamePerson.setText(getLeaveFlowsBySelfEty.getUserName());
		tvtypeLeave.setText(getLeaveFlowsBySelfEty.getLeaveType());
		tvcreateTimeLeave.setText(DateUtil.longToDate(getLeaveFlowsBySelfEty.getWorkFlow().getCreateTime(), 4));
		tvaddLeave.setText(getLeaveFlowsBySelfEty.getStartTime() < getLeaveFlowsBySelfEty.getWorkFlow().getCreateTime() ? "是" : "否");
		//        tvaddLeave.setText(getLeaveFlowsBySelfEty.getIfAdd() == 1 ? "是" : "否");
//		tvstartTimeLeave.setText(DateUtil.longToDate(getLeaveFlowsBySelfEty.getStartTime(), 4));
//		tvendTimeLeave.setText(DateUtil.longToDate(getLeaveFlowsBySelfEty.getEndTime(), 4));
		tvstartTimeLeave.setText(translateTime(getLeaveFlowsBySelfEty.getStartTime()));
		tvendTimeLeave.setText(translateTime(getLeaveFlowsBySelfEty.getEndTime()));

		tvresonLeave.setText(getLeaveFlowsBySelfEty.getLeaveReason());
		tvStateLeave.setText(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName());
		
		doRequestHttp(new GetLeaveFlowNextPerson(getLeaveFlowsBySelfEty.getId()));
	}
	
	@Override
	protected void initListener() {
		btnReject.setOnClickListener(this);
		btnAccept.setOnClickListener(this);
		btnFinish.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		
		if (v == btnReject)
		{
			resDialog = new ResDialog(this, new ResDialogListener()
			{
				@Override
				public void clickOk() {
					progressDialog.show();
					doRequestHttp(new BackLeaveFlowReq(getLeaveFlowsBySelfEty.getId(), resDialog.getEidtText()));
				}
			});
			resDialog.show();
		}
		else if (v == btnAccept)
		{
			if (null != nextuser && getLeaveFlowsBySelfEty.getUserId() != getLeaveFlowsBySelfEty.getNextUserId())
			{
				
				resDialog = new ResDialog(this, new ResDialogListener()
				{
					@Override
					public void clickOk() {
						progressDialog.show();
						doRequestHttp(new AgreeLeaveFlowReq(getLeaveFlowsBySelfEty.getId(), nextuser.getId(), nextuser.getStaffName(), resDialog.getEidtText()));
					}
				});
				resDialog.show();
			}
			else
			{
//				resDialog = new ResDialog(this, new ResDialogListener()
//				{
//					@Override
//					public void clickOk() {
//						progressDialog.show();
//						doRequestHttp(new FinishLeaveFlowReq(getLeaveFlowsBySelfEty.getId(), resDialog.getEidtText()));
//					}
//				});
//				resDialog.show();
				progressDialog.show();
				doRequestHttp(new FinishLeaveFlowReq(getLeaveFlowsBySelfEty.getId(), ""));
				
			}
			
		}
		else if (v == btnFinish)
		{
			doRequestHttp(new FinishLeaveFlowReq(getLeaveFlowsBySelfEty.getId(), ""));
		}
		
		//        switch (v.getId()) {
		//            case R.id.btnReject:
		//                doRequestHttp(new BackLeaveFlowReq(getLeaveFlowsBySelfEty.getId()));
		//                break;
		//            case R.id.btnAccept:
		//                doRequestHttp(new AgreeLeaveFlowReq(getLeaveFlowsBySelfEty.getId(), App.getUser().getId(), App.getUser().getStaffName()));
		//                break;
		//        }
	}
	
	@Override
	public void onResponseSuccess(BaseResponse<?> response, String tag) {
		super.onResponseSuccess(response, tag);
		
		switch (tag) {
			case "GetLeaveFlowNextPerson":
				nextuser = (User) response.getReturnObject();
				progressDialog2.dismiss();
				ll.setVisibility(View.VISIBLE);
				if (nextuser.getUserPositionMark() == 9999) {
					Util.showToast(this, "用户信息不全");
				}
				break;
			case "AgreeLeaveFlowReq":
				showToast("同意审批成功");
				progressDialog.dismiss();
				Intent intent = new Intent();
				setResult(1002, intent);
				finish();
				break;
			case "BackLeaveFlowReq":
				progressDialog.dismiss();
				showToast("拒绝审批成功");
				setResult(1002);
				finish();
				break;
			case "FinishLeaveFlowReq":
				progressDialog.dismiss();
				showToast(nextuser == null?"归档成功":"同意审批成功");
				setResult(1002);
				finish();
				break;
		}
		
	}
	
	@Override
	public void onResponseFail(String tag, int resultCode) {
		super.onResponseFail(tag, resultCode);
		switch (tag) {
			case "GetLeaveFlowNextPerson":
				progressDialog2.dismiss();
				showToast("加载失败，请稍后再试");
				finish();
				break;
			default:
				progressDialog.dismiss();
				showToast("审批失败，请稍后再试");
				break;
		}
		
	}

	/**
	 * 将时间转换为上下午
     */
	private String translateTime(long timeStamp) {
		String time = DateUtil.longToDate(timeStamp, 4);
		String[] strings = time.split(" ");
		if ("09:00:00".equals(strings[1])) {
			return strings[0] + " 上午";
		}
		return strings[0] + " 下午";
	}
}
